<template>
  <view class="container">
    <!-- 搜索栏 -->
    <view class="search-bar">
      <input class="search-input" type="text" placeholder="输入股票代码或名称" v-model="newCode" @input="onInputChange"/>
    </view>
    <!-- 搜索结果下拉列表 -->
    <view v-if="searchResults.length" class="search-dropdown">
      <view class="dropdown-item"  v-for="(item, index) in searchResults"  :key="index" @click="selectStock(item)">{{ item.code }} - {{ item.name }}
      </view>
      </view>

    <!-- 股票列表 -->
    <view v-if="stocks.length === 0" class="empty">
      暂无股票，请输入代码添加
    </view>
    <view class="stock-item" v-for="(stock,index) in stocks" :key="stock.code" @click="gp_data(stock.code)">
      <!-- 股票基本信息 -->
      <view class="row info-row">
        <text class="info-text">股票编号: {{  stock.code.split('.')[1] }}</text>
        <text class="info-text">名称: {{ stock.name }}</text>
      </view>
      <view class="row info-row">
        <text class="info-text">实时价: {{ stock.currentPrice/100 }}</text>
        <text class="info-text">成交量: {{ stock.volume }}</text>
      </view>
      <image class="delete-icon" src="/src/static/del.svg" mode="aspectFit" @click.stop="removeStock(index)"/>
    </view>
  </view>
</template>

<script>
import indexScript from "./index.js";  // 引入外部脚本
export default indexScript;
</script>

<style>
.container {
  padding: 10px;
  background-color: #f5f5f5;
  min-height: 100vh;
}
.search-bar {
  display: flex;
  margin-bottom: 15px;
}
.search-input {
  flex: 1;
  border: 1px solid #ccc;
  border-radius: 6px;
  padding: 6px;
  margin-right: 8px;
}
.empty {
  text-align: center;
  color: #888;
  margin-top: 20px;
  font-size: 14px;
}
.row {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-bottom: 5px;
}
.info-text {
  flex: 1 1 45%;
  min-width: 120px;
  margin-bottom: 5px;
  font-size: 14px;
}
.kline {
  margin-top: 5px;
}
.kline-row {
  font-size: 12px;
  margin-bottom: 3px;
}
.stock-item {
  position: relative;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
.delete-icon {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
}

.search-dropdown {
  position: absolute;
  top: 40px;
  left: 10px;
  width: 90%;
  background: #fff;
  border: 1px solid #ddd;
  z-index: 100;
}
.dropdown-item {
  padding: 6px;
  border-bottom: 1px solid #eee;
}
.dropdown-item:hover {
  background: #f0f0f0;
}
</style>
